package com.gin.danbooru.factory

import com.gin.danbooru.response.DocumentEntity
import okhttp3.ResponseBody
import org.jsoup.nodes.Document
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type

/**
 * @author BX-Gin
 * @from 2024/2/27 10:11
 */
class JsoupConverterFactory : Converter.Factory() {
    override fun responseBodyConverter(
        type: Type,
        annotations: Array<out Annotation>,
        retrofit: Retrofit,
    ): Converter<ResponseBody, *>? {
        if (type is Class<*> && type.genericSuperclass == DocumentEntity::class.java) {
            return DocumentEntityConverter(type as Class<DocumentEntity>)
        }
        if (type == Document::class.java) {
            return DocumentConverter()
        }
        return super.responseBodyConverter(type, annotations, retrofit)
    }
}